package com.infitecs.eyas.mill;

import com.infitecs.eyas.combustion.model.CalculateBurn;
import com.infitecs.eyas.combustion.service.BurnService;
import com.infitecs.eyas.expression.service.ExpressionService;
import com.infitecs.eyas.mill.model.Mill;
import com.infitecs.eyas.mill.model.MillSelect;
import com.infitecs.eyas.mill.model.PipeCalculate;
import com.infitecs.eyas.mill.service.PipeCalculateService;
import com.infitecs.eyas.power.dao.OperateFieldDao;
import com.infitecs.eyas.power.dao.PowerDao;
import com.infitecs.eyas.power.model.OperateField;
import com.infitecs.eyas.power.model.Power;
import com.infitecs.eyas.session.SessionService;
import com.infitecs.eyas.session.model.Param;
import com.infitecs.eyas.step.service.StepService;
import com.infitecs.eyas.tool.Straight;
import com.infitecs.krill.ajax.response.AjaxResponse;
import com.infitecs.krill.ajax.response.ReturnState;
import com.infitecs.krill.exception.ValidateErrorException;
import org.springframework.aop.interceptor.SimpleTraceInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2015/6/28 0028.
 */
@Controller
@RequestMapping("/mill/pipe")
public class PipeCalculateImpl {
    @Autowired
    private PipeCalculateService pipeCalculateService;

   @Autowired
    private SessionService sessionService;

    @Autowired
    private ExpressionService expressionService;
    @Autowired
    private StepService stepService;
    @Autowired
    private OperateFieldDao operateFieldDao;
    @Autowired
    private PowerDao powerDao;
    @RequestMapping("")
    public String getMillPage(HttpServletRequest request) {
        String cId = (String) request.getSession().getAttribute("caseId");
        if (cId == null) {
            return "mill/pipeline";
        }
        int caseId = Integer.parseInt(cId);
        int step = stepService.getMzFxStep(caseId);
        //判断当前方案流程到哪里
        if (step < 6) {
            request.setAttribute("step", step);
            return "mill/pipeline";
        }
        step = stepService.geMmjStep(caseId);
        if (step < 14) {
            request.setAttribute("step", step);
            return "mill/pipeline";
        }
        Power powerTitle = powerDao.getCaseById(cId);
        OperateField operateField = operateFieldDao.getOperateField(caseId);
        PipeCalculate pipeCalculate = pipeCalculateService.getPipeCalculate();
        request.setAttribute("powerTitle", powerTitle);
        request.setAttribute("pipeCalculate", pipeCalculate);
        request.setAttribute("caseId", caseId);
        request.setAttribute("operateField", operateField);
        request.setAttribute("message", request.getParameter("message"));
        return "mill/pipeline";
    }

    @RequestMapping(value = "/saveValueOfPipeCalculate", method = {RequestMethod.POST})
    @ResponseBody
    public void saveValueOfPipeCalculate(Param param) {
        pipeCalculateService.saveValueOfPipeCalculate(param);
    }

    @RequestMapping(value = "/getCalculateInit", method = {RequestMethod.POST})
    @ResponseBody
    public void getCalculateInit() {
        sessionService.putPipeCalculateValue(sessionService.getCaseId());
    }

    @RequestMapping(value = "/calE116AndE117",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Double> calE116AndE117() {
        Map<String,Double> map = new HashMap<>();
        Double E116 = expressionService.calculate(new Param("AE116"));
        Double E117 = expressionService.calculate(new Param("AE117"));
        map.put("E116",E116);
        map.put("E117", E117);
        return map;
    }

    @RequestMapping(value = "/calE120",method = RequestMethod.POST)
    @ResponseBody
    public Double calE120() {
        return expressionService.calculate(new Param("AE120"));
    }

    @RequestMapping(value = "pipelineSave", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResponse pipelineSave(PipeCalculate pipeCalculate) {
        try {
            pipeCalculateService.pipelineSave(pipeCalculate);
            return new AjaxResponse();
        } catch (Exception e) {
            e.printStackTrace();
            if (e instanceof ValidateErrorException) {
                return new AjaxResponse(ReturnState.WARNING, e.getMessage());
            }
            return new AjaxResponse(ReturnState.ERROR, "请检查填写数据是否有误，操作失败");
        }
    }
}
